package com.easymin.weixin.controller.res;

import com.alibaba.fastjson.JSONObject;
import com.easymin.weixin.common.api.DaijiaApi;
import com.easymin.weixin.common.api.PassengerApi;
import com.easymin.weixin.common.api.ZhuanApi;
import com.easymin.weixin.common.api.ZhuanXianApi;
import com.easymin.weixin.entity.WxApplication;
import com.easymin.weixin.entity.area.Area;
import com.easymin.weixin.entity.freight.Order;
import com.easymin.weixin.entity.passenger.Passenger;
import com.easymin.weixin.manager.ErrandManager;
import com.easymin.weixin.manager.FreightManager;
import com.easymin.weixin.manager.WxApplicationManager;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("api/freight")
public class FreightRes {

	private static final Logger logger = LoggerFactory.getLogger(FreightRes.class);

	@Autowired
	private FreightManager freightManager;

	@Autowired
	private WxApplicationManager wxApplicationManager;

	@Autowired
	private DaijiaApi daijiaApi;

	@Autowired
	private ZhuanApi zhuanApi;

	@Autowired
	private ZhuanXianApi zhuanXianApi;

	@Autowired
	private ErrandManager errandManager;

	@Autowired
	private PassengerApi passengerApi;

	/**
	 * 货运下单.
	 */
	@RequestMapping(value = { "create" }, method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> createOrder(@RequestParam("passengerName") String passengerName,
			@RequestParam("passengerPhone") String passengerPhone, @RequestParam("truckTypeId") Long truckTypeId,
			@RequestParam("truckTypeName") String truckTypeName, @RequestParam("channelName") String channelName,
			@RequestParam("startTime") Long startTime, @RequestParam("waypoint") String waypoint,
			@RequestParam(required = false, value = "preStartPrice") Double preStartPrice,
			@RequestParam(required = false, value = "preMileage") Double preMileage,
			@RequestParam(required = false, value = "preRunTime") Integer preRunTime,
			@RequestParam(required = false, value = "preRunTimePrice") Double preRunTimePrice,
			@RequestParam(required = false, value = "budgetPay") Double budgetPay,
			@RequestParam(required = false, value = "passengerId") Long passengerId,
			@RequestParam(required = false, value = "companyId") Long companyId,
			@RequestParam(required = false, value = "companyName") String companyName,
			@RequestParam(required = false, value = "memo") String memo,
			@RequestParam(required = false, value = "flitting") Boolean flitting,
			@RequestParam(required = false, value = "receipt") Boolean receipt,
			@RequestParam(required = false, value = "receivedPay") Boolean receivedPay,
			@RequestParam(required = false, value = "payMount") Double payMount,
			@RequestParam(required = false, value = "preMileagePrice") Double preMileagePrice,
			@RequestParam(required = false, value = "couponId") Long couponId, // 优惠券id
			@RequestParam(required = false, value = "payDetail") String payDetail,
			@RequestParam(required = false, value = "tip") Double tip, @RequestParam("appKey") String appKey)
			throws Exception {

		appKey = StringUtils.trimToEmpty(appKey);
		Map<String, Object> map = new HashMap<String, Object>();
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);

		Passenger passenger = passengerApi.findById(passengerId, appKey);
		// 哒哒专属，判断客户
		if (appKey.equals("a1f4d7d78a854565930fcee06e2b4d1d")) {
			boolean istrue = daijiaApi.isCreateOrderNumberFreight(wxApplication.getAddress(), appKey, passengerId);
			if (!istrue) {
				map.put("code", -55);
				map.put("msg", "您今日下单已达到上限，继续下单请联系客服。");
				return map;
			}
		} else {
			Long runNumber = freightManager.countRunning(passengerId, appKey);
			if (runNumber > 10) {
				map.put("code", -55);
				map.put("msg", "您今日下单已达到上限，继续下单请联系客服。");
				return map;
			}
		}
		// 并行单量判断
		Long count = freightManager.countNotFinishedByPassenger(passengerId, appKey);
		Long number = 1L;
		if (null != passenger.getPassengerGrade() && passenger.getPassengerGrade().getParallelOrderNumber() > 0
				&& count + number > passenger.getPassengerGrade().getParallelOrderNumber()) {
			map.put("code", -86);
			map.put("msg", "您当前有" + count + "个订单未结束!不能再下新单。");
			return map;
		}

		JSONObject object = daijiaApi.getServices(wxApplication.getAddress());
		if (object != null) {
			Long num = 0L;
			boolean daijia = object.getBooleanValue("daijia");
			if (daijia) {
				num += daijiaApi.countNotPayAmounts(wxApplication.getAddress(), passengerId);
			}
			boolean zhuanche = object.getBooleanValue("zhuanche");
			if (zhuanche) {
				num += zhuanApi.countNotPayAmounts(passengerId, appKey);
			}
			boolean paotui = object.getBooleanValue("paotui");
			if (paotui) {
				num += errandManager.countNotPayAmounts(passengerId, appKey);
			}
			boolean freight = object.getBooleanValue("freight");
			if (freight) {
				num += freightManager.countNotPayAmounts(passengerId, appKey);
			}
			boolean zhuanxian = object.getBooleanValue("zhuanxian");
			if (zhuanxian) {
				num += zhuanXianApi.countNotPayAmounts(passengerId, appKey);
			}
			if (num > 0) {
				map.put("code", -54);
				map.put("msg", "还有未完成支付的订单，不能下单");
				return map;
			}
		}
		if (null == channelName || channelName.isEmpty() || "APP".equals(channelName)) {
			channelName = "APP预约";
		}
		map = freightManager.create(passengerId, passengerName, passengerPhone, companyId, companyName, truckTypeId,
				truckTypeName, channelName, startTime, waypoint, memo, flitting, receipt, receivedPay, payMount,
				preMileagePrice, couponId, tip, preStartPrice, preMileage, preRunTime, preRunTimePrice, budgetPay,
				payDetail, appKey);
		int code = (int) map.get("code");
		if (code == 0) {
			Order order = (Order) map.get("data");
			try {
				daijiaApi.pushFreightNewOrder(wxApplication.getAddress(), order.getId()); // 新单推送
			} catch (Exception ex) {
				logger.error(ex.getMessage());
			}
		}
		return map;
	}

	/**
	 * 查询货运车辆类型.
	 * 
	 * @param acKey
	 *            区别公司
	 * @param lng
	 *            客户所在地经度
	 * @param lat
	 *            客户所在地纬度
	 * @param appKey
	 *            区分系统
	 */
	@RequestMapping(value = { "findTruckType" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> findTruckType(
			@RequestParam(required = false, value = "acKey") String acKey, @RequestParam("lng") Double lng,
			@RequestParam("lat") Double lat, @RequestParam("appKey") String appKey) throws Exception {
		appKey = StringUtils.trimToEmpty(appKey);
		Map<String, Object> map = new HashMap<String, Object>();
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		try {
			map = daijiaApi.findTruckType(wxApplication.getAddress(), acKey, lng, lat);
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "内部服务器错误");
		}
		return map;
	}

	/**
	 * 替代findTruckType接口去查询货运类型.
	 */
	@RequestMapping(value = { "queryTruckType" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> queryTruckType(@RequestParam("companyId") Long companyId,
			@RequestParam("appKey") String appKey) throws Exception {
		appKey = StringUtils.trimToEmpty(appKey);
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map = daijiaApi.queryTruckType(wxApplication.getAddress(), companyId);
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "内部服务器错误");
		}
		return map;
	}

	/**
	 * 预算货车价格.
	 * 
	 * @param areaId
	 *            收费标准Id
	 * @param time
	 *            预算时间
	 * @param mileage
	 *            预计里程
	 * @param appKey
	 *            区分系统
	 * @return 预算价格的相关信息
	 */
	@RequestMapping(value = { "getPrice" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> getPrice(@RequestParam("areaId") Long areaId,
			@RequestParam("time") Long time, @RequestParam("mileage") Double mileage,
			@RequestParam("appKey") String appKey) throws Exception {
		appKey = StringUtils.trimToEmpty(appKey);
		Map<String, Object> map = new HashMap<String, Object>();
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);

		Map<String, Object> map2 = daijiaApi.getFreightPrice(wxApplication.getAddress(), areaId, time, mileage);
		if (map2.get("success").equals(true)) {
			map.put("code", 0);
			map.put("msg", "成功");
			map.put("data", map2);
		} else {
			map.put("code", -1);
			map.put("msg", map2.get("message"));
		}
		return map;
	}

	/**
	 * 查询常用路线.
	 */
	@RequestMapping(value = { "findRoadLine" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> findRoadLine(@RequestParam("passengerId") Long passengerId,
			@RequestParam("appKey") String appKey) throws Exception {
		appKey = StringUtils.trimToEmpty(appKey);
		Map<String, Object> map = new HashMap<String, Object>();

		map = freightManager.findRoadLine(passengerId, appKey);
		return map;
	}

	/**
	 * 保存常用路线.
	 */
	@RequestMapping(value = { "saveRoadLine" }, method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> saveRoadLine(@RequestParam("waypoints") String waypoints,
			@RequestParam("passengerId") Long passengerId, @RequestParam("addressName") String addressName,
			@RequestParam("appKey") String appKey) throws Exception {
		appKey = StringUtils.trimToEmpty(appKey);
		Map<String, Object> map = new HashMap<String, Object>();
		map = freightManager.saveRoadLine(waypoints, passengerId, addressName, appKey);
		return map;
	}

	/**
	 * 删除常用路线.
	 */
	@RequestMapping(value = { "deleteRoadLine" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> deleteRoadLine(@RequestParam("id") Long id,
			@RequestParam("appKey") String appKey) throws Exception {
		appKey = StringUtils.trimToEmpty(appKey);
		Map<String, Object> map = new HashMap<String, Object>();
		map = freightManager.deleteRoadLine(id, appKey);
		return map;
	}

	/**
	 * 查询附近货运司机.
	 */
	@RequestMapping(value = { "findDrivers" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> findDrivers(@RequestParam("lng") Double lng,
			@RequestParam("lat") Double lat, @RequestParam("truckTypeId") Long truckTypeId,
			@RequestParam("appKey") String appKey) throws Exception {
		appKey = StringUtils.trimToEmpty(appKey);
		Map<String, Object> map = new HashMap<String, Object>();
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		try {
			map = daijiaApi.findDrivers(wxApplication.getAddress(), lng, lat, truckTypeId);
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "内部服务器错误");
		}
		return map;
	}

	/**
	 * 查询货车位置.
	 */
	@RequestMapping(value = { "queryFreightCar" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> findDriver(@RequestParam("employId") Long driverId,
			@RequestParam("appKey") String appKey) throws Exception {
		appKey = StringUtils.trimToEmpty(appKey);

		Map<String, Object> map = new HashMap<String, Object>();
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		try {
			map = daijiaApi.queryFreightCar(wxApplication.getAddress(), driverId);
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "内部服务器错误");
		}
		return map;
	}

	/**
	 * 查询单个订单.
	 */
	@RequestMapping(value = { "findByOrderId" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> findByOrderId(@RequestParam("orderId") Long orderId,
			@RequestParam("appKey") String appKey) throws Exception {
		appKey = StringUtils.trimToEmpty(appKey);
		Map<String, Object> map = new HashMap<String, Object>();
		map = freightManager.findByOrderId(orderId, appKey);
		return map;
	}

	/**
	 * 查询客户订单(分页).
	 */
	@RequestMapping(value = { "findOrders" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> findOrders(@RequestParam("passengerId") Long passengerId,
			@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "rows", required = false) Integer rows, @RequestParam("appKey") String appKey)
			throws Exception {
		appKey = StringUtils.trimToEmpty(appKey);
		Map<String, Object> map = new HashMap<String, Object>();

		map = freightManager.findOrders(passengerId, page, rows, appKey);
		return map;
	}

	/**
	 * 评价订单.
	 * 
	 * @param orderId
	 *            订单Id
	 * @param content
	 *            评价内容
	 * @param score
	 *            评价分数
	 * @param appKey
	 *            区分系统
	 */
	@RequestMapping(value = { "review" }, method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> review(@RequestParam("orderId") Long orderId,
			@RequestParam(required = false, value = "content") String content, @RequestParam("score") Double score,
			@RequestParam("appKey") String appKey) throws Exception {
		appKey = StringUtils.trimToEmpty(appKey);

		Map<String, Object> map = new HashMap<String, Object>();
		map = freightManager.review(orderId, content, score, appKey);
		return map;
	}

	/**
	 * 催一下订单进度.
	 */
	@RequestMapping(value = { "reminder" }, method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> reminder(@RequestParam("orderId") Long orderId,
			@RequestParam("appKey") String appKey) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		appKey = StringUtils.trimToEmpty(appKey);
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		Order order = freightManager.findOneOrder(orderId, appKey);
		if (null == order) {
			map.put("code", -1);
			map.put("msg", "订单不存在");
			return map;
		}
		if (order.getStatus() == 5 || order.getStatus() == 6) {
			map.put("code", -2);
			map.put("msg", "订单已完成,或已销单,催促无效");
			return map;
		}
		if (order.getLasturge() != null) {
			if ((new Date().getTime() - order.getLasturge().getTime()) < (2 * 60 * 1000)) {
				map.put("code", -1);
				map.put("msg", "订单刚催促过,两分钟内不能重复操作");
				return map;
			}
		}
		try {
			String content = freightManager.hasten(orderId, appKey); // 调用货运系统更新上次催促时间
			if ("success".equals(content)) {
				daijiaApi.hastenFreightOrder(wxApplication.getAddress(), orderId); // 调用代驾的接口让其显示货运催单消息
				map.put("code", 0);
				if (order.getCreated().getTime() == order.getLasturge().getTime()) {
					map.put("msg", "我们正在为您安排服务人员");
				} else {
					map.put("msg", "消息已发出,管理员已经知道了");
				}
				map.put("data", null);
			} else if ("fail".equals(content)) {
				map.put("code", -1);
				map.put("msg", "催单失败");
			}
			return map;
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "催促操作失败");
			return map;
		}
	}

	/**
	 * 销单(客户端).
	 */
	@RequestMapping(value = { "cancel" }, method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> cancel(@RequestParam("orderId") Long orderId,
			@RequestParam("cause") String cause, @RequestParam(required = false, value = "isDriver") Boolean isDriver,
			@RequestParam("appKey") String appKey) throws Exception {
		appKey = StringUtils.trimToEmpty(appKey);

		Map<String, Object> map = new HashMap<String, Object>();
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);

		if (null == isDriver) {
			isDriver = Boolean.FALSE;
		}

		map = freightManager.cancel(orderId, cause, isDriver, appKey);
		int code = (int) map.get("code");
		if (code == 0) {
			try {
				daijiaApi.pushOrderCancel(wxApplication.getAddress(), orderId, isDriver); // 销单推送
			} catch (Exception ex) {
				logger.error(ex.getMessage());
			}
		}
		return map;
	}

	/**
	 * 销单(司机端).
	 */
	@RequestMapping(value = { "employCancel" }, method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> employCancel(@RequestParam("orderId") Long orderId,
			@RequestParam("cause") String cause, @RequestParam("appKey") String appKey) throws Exception {
		appKey = StringUtils.trimToEmpty(appKey);

		Map<String, Object> map = new HashMap<String, Object>();
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		map = freightManager.cancel(orderId, cause, true, appKey);
		int code = (int) map.get("code");
		if (code == 0) {
			try {
				daijiaApi.pushOrderCancel(wxApplication.getAddress(), orderId, true); // 销单推送
			} catch (Exception ex) {
				logger.error(ex.getMessage());
			}
		}
		return map;
	}

	/**
	 * 接单接口.
	 */
	@RequestMapping(value = { "jiedan" }, method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> jiedan(@RequestParam("orderId") Long orderId,
			@RequestParam("appKey") String appKey) throws Exception {
		appKey = StringUtils.trimToEmpty(appKey);
		Map<String, Object> map = new HashMap<String, Object>();
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		map = freightManager.jiedan(orderId, appKey);
		int code = (int) map.get("code");
		if (code == 0) {
			try {
				daijiaApi.pushOrderAccept(wxApplication.getAddress(), orderId); // 接单推送
			} catch (Exception ex) {
				logger.error(ex.getMessage());
			}
		}
		return map;
	}

	/**
	 * 拒单接口.
	 */
	@RequestMapping(value = { "refuse" }, method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> refuse(@RequestParam("orderId") Long orderId,
			@RequestParam("employId") Long employId, @RequestParam("cause") String cause,
			@RequestParam("appKey") String appKey) throws Exception {
		appKey = StringUtils.trimToEmpty(appKey);
		Map<String, Object> map = new HashMap<String, Object>();
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		map = freightManager.refuse(orderId, employId, cause, appKey);
		int code = (int) map.get("code");
		if (code == 0) {
			try {
				daijiaApi.pushRefuseFreight(wxApplication.getAddress(), orderId, employId, cause);
			} catch (Exception ex) {
				logger.error(ex.getMessage());
			}
		}
		return map;
	}

	/**
	 * 抢单接口.
	 */
	@RequestMapping(value = { "grabOrder" }, method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> grabOrder(@RequestParam("orderId") Long orderId,
			@RequestParam("employId") Long employId, @RequestParam("appKey") String appKey) throws Exception {
		appKey = StringUtils.trimToEmpty(appKey);

		Map<String, Object> map = new HashMap<String, Object>();
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);

		Map<String, Object> map2 = daijiaApi.queryDriver(wxApplication.getAddress(), employId);
		Boolean success = (Boolean) map2.get("success");
		if (success) {
			String employName = (String) map2.get("employName");
			String employPhone = (String) map2.get("employPhone");
			String employNo = (String) map2.get("employNo");
			Long employCompanyId = (Long) map2.get("employCompanyId");
			String employPhoto = (String) map2.get("employPhoto");
			Double employScore = (Double) map2.get("employScore");

			map = freightManager.grabOrder(orderId, employId, employName, employPhone, employNo, employCompanyId,
					employPhoto, employScore, appKey);
			int code = (int) map.get("code");
			if (code == 0) {
				Order order = (Order) map.get("data");
				Long areaId = order.getTruckType().getAreaId();
				WxApplication application = wxApplicationManager.findByAppKey(appKey);
				Area area = daijiaApi.queryArea(application.getAddress(), areaId);
				order.setArea(area);
				map.put("data", order);
				try {
					daijiaApi.pushOrderAccept(wxApplication.getAddress(), orderId);
				} catch (Exception ex) {
					logger.error(ex.getMessage());
				}
			}

		} else {
			map.put("code", -1);
			map.put("msg", map2.get("message"));
		}

		return map;
	}

	/**
	 * 开始等待.
	 */
	@RequestMapping(value = { "wait" }, method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> wait(@RequestParam("orderId") Long orderId,
			@RequestParam("appKey") String appKey) throws Exception {
		appKey = StringUtils.trimToEmpty(appKey);

		Map<String, Object> map = new HashMap<String, Object>();
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		map = freightManager.wait(orderId, appKey);
		int code = (int) map.get("code");
		if (code == 0) {
			try {
				daijiaApi.pushWait(wxApplication.getAddress(), orderId); // 到达预约地推送
			} catch (Exception ex) {
				logger.error(ex.getMessage());
			}
		}

		return map;
	}

	/**
	 * 订单出发接口.
	 */
	@RequestMapping(value = { "runOrder" }, method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> runOrder(@RequestParam("orderId") Long orderId,
			@RequestParam("appKey") String appKey) throws Exception {
		appKey = StringUtils.trimToEmpty(appKey);

		Map<String, Object> map = new HashMap<String, Object>();
		map = freightManager.runOrder(orderId, appKey);
		return map;
	}

	/**
	 * 到达某个途经点.
	 */
	@RequestMapping(value = { "reach" }, method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> reach(@RequestParam("orderId") Long orderId,
			@RequestParam("waypointId") Long waypointId, @RequestParam("appKey") String appKey) throws Exception {
		appKey = StringUtils.trimToEmpty(appKey);
		Map<String, Object> map = new HashMap<String, Object>();
		map = freightManager.reach(orderId, waypointId, appKey);
		return map;
	}

	/**
	 * 确认货运订单费用的接口.
	 * 
	 * @param orderId
	 *            订单id
	 * @param realCash
	 *            实付金额
	 * @param guoluMoney
	 *            过路费
	 * @param yuanchengMoney
	 *            远程费
	 * @param otherMoney
	 *            其他费
	 * @param mileage
	 *            里程
	 * @param waitedTime
	 *            等待时间
	 * @param waitPrice
	 *            等待时间费用
	 * @param travelTime
	 *            行驶时间
	 * @param xml
	 *            报单数据
	 */
	@RequestMapping(value = { "confirmTask" }, method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> complete(@RequestParam("orderId") Long orderId,
			@RequestParam("realCash") Double realCash, @RequestParam("guoluMoney") Double guoluMoney,
			@RequestParam("yuanchengMoney") Double yuanchengMoney, @RequestParam("otherMoney") Double otherMoney,
			@RequestParam("mileage") Double mileage, @RequestParam("waitedTime") Integer waitedTime,
			@RequestParam("travelTime") Integer travelTime, @RequestParam("xml") String xml,
			@RequestParam("startPrice") Double startPrice, @RequestParam("mileagePrice") Double mileagePrice,
			@RequestParam("runTimePrice") Double runTimePrice,
			@RequestParam(required = false, value = "waitPrice") Double waitPrice,
			@RequestParam(required = false, value = "memo") String memo, @RequestParam("appKey") String appKey) {
		Map<String, Object> map = new HashMap<String, Object>();
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		try {
			Order order = freightManager.findOneOrder(orderId, appKey);
			if (order.getStatus() == 6) {
				map.put("code", -2);
				map.put("msg", "订单已经取消");
				return map;
			}
			map = daijiaApi.confirmTaskFreight(wxApplication.getAddress(), orderId, realCash, guoluMoney,
					yuanchengMoney, otherMoney, mileage, waitedTime, travelTime, xml, startPrice, mileagePrice,
					runTimePrice, waitPrice, memo);
			return map;
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "确认货运订单费用失败");
			return map;
		}
	}

	/**
	 * 货运订单结算页面的数据.
	 */
	@RequestMapping(value = { "completeTask" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> completeTask(@RequestParam("orderId") Long orderId,
			@RequestParam("appKey") String appKey) {
		Map<String, Object> map = new HashMap<String, Object>();
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		try {
			map = daijiaApi.completeTaskFreight(wxApplication.getAddress(), orderId);
			return map;
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "获取货运结算页面的数据失败");
			return map;
		}
	}

	/**
	 * 实时获取订单是否被结算.
	 */
	@RequestMapping(value = { "checkTask" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> checkTask(@RequestParam("orderId") Long orderId,
			@RequestParam("appKey") String appKey) {
		Map<String, Object> map = new HashMap<String, Object>();
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		try {
			map = daijiaApi.checkTaskFreight(wxApplication.getAddress(), orderId);
			return map;
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "实时获取货运订单是否被结算失败!");
			return map;
		}
	}

	/**
	 * 最后结算接口.
	 * 
	 * @param payType
	 *            支付类型: helppay(代付) sign(签单) balance(余额)
	 */
	@RequestMapping(value = { "finishTask" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> finishTask(@RequestParam("orderId") Long orderId,
			@RequestParam("payType") String payType, @RequestParam("appKey") String appKey) {
		Map<String, Object> map = new HashMap<String, Object>();
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		try {
			map = daijiaApi.finishTaskFreight(wxApplication.getAddress(), orderId, payType);
			return map;
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "货运结算提交失败!");
			return map;
		}
	}

	/**
	 * 货运订单客户端支付所需数据.
	 */
	@RequestMapping(value = { "clientPayData" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> clientPayData(@RequestParam("orderId") Long orderId,
			@RequestParam("appKey") String appKey) {
		Map<String, Object> map = new HashMap<String, Object>();
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		try {
			map = daijiaApi.clientPayDataFreight(wxApplication.getAddress(), orderId);
			return map;
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "获取货运结算页面的数据失败");
			return map;
		}
	}

	/**
	 * 客户端支付宝结算货运订单.
	 */
	@RequestMapping(value = { "aliAfterPay" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> aliAfterpay(@RequestParam("orderId") Long orderId,
			@RequestParam("appKey") String appKey) {
		Map<String, Object> map = new HashMap<String, Object>();
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		try {
			map = daijiaApi.aliAfterpayFreight(wxApplication.getAddress(), orderId);
			return map;
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "获取货运结算页面的数据失败");
			return map;
		}
	}

	/**
	 * 客户端微信结算货运订单.
	 */
	@RequestMapping(value = { "wxAfterPay" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> wxAfterpay(@RequestParam("orderId") Long orderId,
			@RequestParam("appKey") String appKey) {
		Map<String, Object> map = new HashMap<String, Object>();
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		try {
			map = daijiaApi.wxAfterpayFreight(wxApplication.getAddress(), orderId);
			return map;
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "获取货运结算页面的数据失败");
			return map;
		}
	}
}
